import common.TreeNode;

/**
 * @author tmh
 * @date 2024/9/28 19:46
 * @description
 */
public class T543二叉树的直径 {
    private int maxLen = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return maxLen;
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return -1;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        maxLen = Math.max(maxLen, left + right+ 2) ;
        return Math.max(left, right) + 1;
    }

}
